<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.io.*,java.text.*,java.util.*,java.sql.*,javax.servlet.*,javax.servlet.http.*,DBstep.iDBManager2000.*,com.thx.common.util.AppContext" %>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="thx" uri="/thx-tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  ResultSet result=null;

  String mDescript="";
  String mFileName="";

  String mHttpUrlName=request.getRequestURI();
  String mScriptName=request.getServletPath();
  String mServerName="workflow/webOffice/OfficeServer.jsp";
  String mClientName="workflow/webOffice/iWebOffice2003.ocx#version=8,8,5,2";
  String mServerUrl="http://"+request.getServerName()+":"+request.getServerPort()+mHttpUrlName.substring(0,mHttpUrlName.lastIndexOf(mScriptName))+"/"+mServerName;
  String mClientUrl="http://"+request.getServerName()+":"+request.getServerPort()+mHttpUrlName.substring(0,mHttpUrlName.lastIndexOf(mScriptName))+"/"+mClientName;
  String mRecordID=request.getParameter("RecordID");
  String mFileType=request.getParameter("FileType");
  String companyId = AppContext.getCurrentComId();
  String mEditType="1";
  String mUserName="Administrator";


  //取得模式
  if ( mEditType==null)
  {
    mEditType="2";		// 2 起草
  }
  //取得类型
  if ( mFileType==null)
  {
    mFileType=".doc";	// 默认为.doc文档
  }
  //取得用户名
  if (mUserName==null)
  {
    mUserName="金格科技";
  }

  //取得模板
  if ( mRecordID==null)
  {
    mRecordID="";	// 默认没有模板
  }

  //打开数据库
  DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
  if (DbaObj.OpenConnection())
  {
    String mSql="Select * From Arc_Template_File Where RecordID='"+ mRecordID + "'  and company_id = '"+companyId+"'";
    try
    {
      result=DbaObj.ExecuteQuery(mSql);
      if (result.next())
      {
        mRecordID=result.getString("RecordID");
        mFileName=result.getString("FileName");
        mFileType=result.getString("FileType");
        mDescript=result.getString("Descript");
      }
      else
      {
	//取得唯一值(mRecordID)
        java.util.Date dt=new java.util.Date();
        long lg=dt.getTime();
        Long ld=new Long(lg);
	//初始化值
        mRecordID=ld.toString();
        mFileName="公文模版"+mFileType;
        mFileType=mFileType;
        mDescript="发文公文模版";
      }
      result.close();
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
    DbaObj.CloseConnection() ;
  }

%>

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
	    <meta charset="utf-8">
	    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
	    <meta name="viewport" content="width=device-width, initial-scale=1">
	    <meta content="" name="description">
	    
	    <!-- 双核浏览器 -->
	    <meta content="webkit" name="renderer">
	    
	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="pragma" content="no-cache"/> 
		<meta http-equiv="cache-control" content="no-cache"/> 
		<meta http-equiv="expires" content="0"/>
	    <title>模板管理</title>

	    <link href="${pageContext.request.contextPath}/css/font-awesome.min.css" rel="stylesheet"/>
	    <link href="${pageContext.request.contextPath}/css/ionicons.min.css" rel="stylesheet"/>
	    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet"/>
	    <link href="${pageContext.request.contextPath}/css/AdminLTE/skins/skin-blue.min.css" rel="stylesheet"/>
	    <link href="${pageContext.request.contextPath}/css/AdminLTE/AdminLTE.min.css" rel="stylesheet"/>
	    <link href="${pageContext.request.contextPath}/css/common.css" rel="stylesheet"/>
	    
		<style type="text/css">
			.handle {
				background: #fff;
				width: 100%;
				height: 40px;
				line-height: 36px;
				vertical-align: middle;
			}
			.handle>.a-btn {
			    background: #666 none repeat scroll 0 0;
			    border: 0 none;
			    border-radius: 3px;
			    color: #fff;
			    cursor: pointer;
			    font-size: 12px;
			    margin-right: 10px;
			    padding: 7px 16px;
			    text-align: center;
			    transition: all 0.1s ease-in 0s;
			    vertical-align: middle;
			}
			.wrapper .content-wrapper .content .row .box .box-body .table-bordered th{
				width:130px;
				vertical-align:middle;
			}
			.wrapper .content-wrapper .content .row .box .box-body .table-bordered td{
				vertical-align:middle;
			}
		</style>

		<script language="javascript" for=WebOffice event="OnMenuClick(vIndex,vCaption)">
		   if (vIndex==1){  //打开本地文件
		      WebOpenLocal();
		   }
		   if (vIndex==2){  //保存本地文件
		      WebSaveLocal();
		   }
		   if (vIndex==4){  //保存并退出
		     SaveDocument();    //保存正文
		     webform.submit();  //提交表单
		   }
		   if (vIndex==6){  //打印文档
		      WebOpenPrint();
		   }
		</script>
	
		<script language="javascript">
	
			//作用：显示操作状态
			function StatusMsg(mString){
			  StatusBar.innerText=mString;
			}
			
			//作用：载入iWebOffice
			function Load(){
			  try{
			
			  //以下属性必须设置，实始化iWebOffice
			  webform.WebOffice.WebUrl="<%=mServerUrl%>";    //WebUrl:系统服务器路径，与服务器文件交互操作，如保存、打开文档，重要文件
			  webform.WebOffice.RecordID="<%=mRecordID%>";   //RecordID:本文档记录编号
			  webform.WebOffice.Template="<%=mRecordID%>";   //Template:模板编号
			  webform.WebOffice.FileName="<%=mFileName%>";   //FileName:文档名称
			  webform.WebOffice.FileType="<%=mFileType%>";   //FileType:文档类型  .doc  .xls  .wps
			  webform.WebOffice.EditType="<%=mEditType%>";   //EditType:编辑类型  方式一、方式二  <参考技术文档>
			  webform.WebOffice.UserName="<%=mUserName%>";   //UserName:操作用户名
			  
			  //以下属性可以不要
			  webform.WebOffice.ShowMenu="1";  //ShowMenu:1 显示菜单  0 隐藏菜单
			  webform.WebOffice.AppendMenu("1","打开本地文件(&L)");
			  webform.WebOffice.AppendMenu("2","保存本地文件(&S)");
			  webform.WebOffice.AppendMenu("3","-");
			  webform.WebOffice.AppendMenu("4","保存并退出(&E)");
			  webform.WebOffice.AppendMenu("5","-");
			  webform.WebOffice.AppendMenu("6","打印文档(&P)");
			  webform.WebOffice.DisableMenu("宏;选项;帮助");  //禁止菜单
			
			  webform.WebOffice.WebOpen();  	//打开该文档    交互OfficeServer的OPTION="LOADTEMPLATE"
			  StatusMsg(webform.WebOffice.Status);
			  }catch(e){}
			}
			
			//作用：退出iWebOffice
			function UnLoad(){
			  try{
			  if (!webform.WebOffice.WebClose()){
			     StatusMsg(webform.WebOffice.Status);
			  }else{
			     StatusMsg("关闭文档...");
			  }
			  }catch(e){}
			}
			
			
			//作用：打开文档
			function LoadDocument(){
			  StatusMsg("正在打开文档...");
			  alert(webform.WebOffice.Status);
			  alert(webform.WebOffice.WebLoadTemplate());
			  if (!webform.WebOffice.WebLoadTemplate()){  //交互OfficeServer的OPTION="LOADTEMPLATE"
			     StatusMsg(webform.WebOffice.Status);
			  }else{
			     StatusMsg(webform.WebOffice.Status);
			  }
			}
			
			//作用：保存文档
			function SaveDocument(){
			  webform.WebOffice.WebClearMessage();            //清空iWebOffice变量
			  if (!webform.WebOffice.WebSaveBookMarks()){    //交互OfficeServer的OPTION="SAVEBOOKMARKS"
			     StatusMsg(webform.WebOffice.Status);
			     return false;
			  }
			  //webform.WebOffice.WebSetMsgByName("MyDefine1","自定义变量值1");  //设置变量MyDefine1="自定义变量值1"，变量可以设置多个  在WebSaveTemplate()时，一起提交到OfficeServer中
			  if (!webform.WebOffice.WebSaveTemplate()){    //交互OfficeServer的OPTION="SAVETEMPLATE"
			     StatusMsg(webform.WebOffice.Status);
			     return false;
			  }else{
			     StatusMsg(webform.WebOffice.Status);
			     return true;
			  }
			}
			
			//作用：填充模板
			function LoadBookmarks(){
			  StatusMsg("正在填充模扳...");
			  alert(webform.WebOffice.Status);
			  if (!webform.WebOffice.WebLoadBookmarks()){    //交互OfficeServer的OPTION="LOADBOOKMARKS"
			     StatusMsg(webform.WebOffice.Status);
			  }else{
			     StatusMsg(webform.WebOffice.Status);
			  }
			}
			
			//作用：设置书签值  vbmName:标签名称，vbmValue:标签值   标签名称注意大小写
			function SetBookmarks(vbmName,vbmValue){
			  if (!webform.WebOffice.WebSetBookmarks(vbmName,vbmValue)){
			     StatusMsg(webform.WebOffice.Status);
			  }else{
			     StatusMsg(webform.WebOffice.Status);
			  }
			}
			
			//作用：根据标签名称获取标签值  vbmName:标签名称
			function GetBookmarks(vbmName){
			  var vbmValue;
			  vbmValue=webform.WebOffice.WebGetBookmarks(vbmName);
			  return vbmValue;
			}
			
			//作用：打印文档
			function WebOpenPrint(){
			  try{
			    webform.WebOffice.WebOpenPrint();
			    StatusMsg(webform.WebOffice.Status);
			  }catch(e){}
			}
			
			//作用：页面设置
			function WebOpenPageSetup(){
			   try{
				if (webform.WebOffice.FileType==".doc"){
				  webform.WebOffice.WebObject.Application.Dialogs(178).Show();
				}
				if(webform.WebOffice.FileType==".xls"){
				  webform.WebOffice.WebObject.Application.Dialogs(7).Show();
				}
			   }catch(e){
			
			   }
			}
			
			//作用：标签管理
			function WebOpenBookMarks(){
			  try{
			    webform.WebOffice.WebOpenBookmarks();    //交互OfficeServer的OPTION="LISTBOOKMARKS"
			    StatusMsg(webform.WebOffice.Status);
			  }catch(e){}
			}
			
			//作用：存为本地文件
			function WebSaveLocal(){
			  try{
			    webform.WebOffice.WebSaveLocal();
			    StatusMsg(webform.WebOffice.Status);
			  }catch(e){}
			}
			
			//作用：打开本地文件
			function WebOpenLocal(){
			  try{
			    webform.WebOffice.WebOpenLocal();
			    StatusMsg(webform.WebOffice.Status);
			  }catch(e){}
			}
			
			function SaveTemp(){
				SaveDocument();
				
				//document.webform.action="TemplateSave.jsp";
				//document.webform.submit();
				var fileName = $("#FileName").val();
				if($.trim(fileName)==""){
					alert("模板名不能为空！");
				}else{
					$.ajax({
					    type : "POST",
						url : "${pageContext.request.contextPath }/workflow/template!update.action",
						data: "recordId="+$("#RecordID").val()+"&name="+$("#FileName").val()+"&descript="+$("#Descript").val(),				
						success : function(msg){
							if(msg.code=="fail"){
								alert(msg.desc);
							}else if(msg.code=="success"){
								//window.location.reload();
								alert(msg.desc);
							}
						}
					
					});
				} 
			}
			function CloseWin(){
				if(confirm("确定关闭？")){
					window.close();
				}
			}
			
			$(".oa_list").height($(window).height()-50);
			$("#WebOffice").height($(".oa_list").height()-100);
		</script>
	</head>
	<body class="hold-transition skin-blue sidebar-mini" onload="Load()" onunload="UnLoad()"><!--引导和退出iWebOffice-->
		<div class="wrapper">
			<header class="handle" style="border-bottom: 3px solid #3c8dbc;">
				<a title="定义标签" class="a-btn" href="javascript:void(0);" onclick="WebOpenBookMarks()" style="margin-right: 5px; margin-left: 5px"> 
					<span class="text">定义标签</span>
				</a>
				<%-- <a title="填充模版" class="a-btn" href="javascript:void(0);" onclick="LoadBookmarks()" style="margin-right: 5px"> 
					<span class="text">填充模版</span>
				</a>
				<a title="重调文档" class="a-btn" href="javascript:void(0);" onclick="LoadDocument()" style="margin-right: 5px"> 
					<span class="text">重调文档</span>
				</a> --%>
				<a title="打开文件" class="a-btn" href="javascript:void(0);" onclick="WebOpenLocal()" style="margin-right: 5px"> 
					<span class="text">打开文件</span>
				</a>
				<a title="保存到本地" class="a-btn" href="javascript:void(0);" onclick="WebSaveLocal()" style="margin-right: 5px"> 
					<span class="text">保存到本地</span>
				</a>
				<a title="保存文档" class="a-btn" href="javascript:void(0);" onclick="SaveTemp()" style="margin-right: 5px"> 
					<span class="text">保存文档</span>
				</a>
				<a title="关闭" class="a-btn" href="javascript:void(0);" onclick="CloseWin()" > 
					<span class="text">关闭</span>
				</a>
		    </header>
		    <div class="content-wrapper" style="margin-left: 0px; padding-top: 40px;">
		    	<section class="content" style="width: 70%;">
					<div class="row">
						<div class="col-md-12">
						   <div class="box">
					   		  <form name="webform" method="post" action="" onsubmit="return SaveDocument();"> <!--保存iWebOffice后提交表单信息,暂时没有使用-->
						   		 <input type="hidden" name="RecordID" id="RecordID" value="<%=mRecordID%>" />
								 <input type="hidden" name="comanyId" id="companyId" value="<%=companyId%>" />
						         <table border=0 cellspacing='0' cellpadding='0' width=100% height=100%  align=center class="">
									<tr>
									  <td align="right" class="" width=64 height="20px">模版名&nbsp;:&nbsp;</td>
									  <td><input type="text" id="FileName" name="FileName" size ="50" value="<%=mFileName%>" class="form-control" ></td>
									  <td align=right class="" width=64>说明&nbsp;:&nbsp;</td>
									  <td><input type="text" id="Descript" name="Descript" size ="50" value="<%=mDescript%>" class="form-control" ></td>
									</tr>
									<tr>
									  <td colspan="4">
										<table border=0 cellspacing='0' cellpadding='0' width='100%' id="tableWebOffice">
									        <tr>
									          <td bgcolor="menu" colspan="2" id="tdWebOffice"><!--调用iWebOffice，注意版本号，可用于升级-->
								                <OBJECT id="WebOffice" width="100%" height="" classid="clsid:23739A7E-5741-4D1C-88D5-D50B18F7C347" codebase="<%=mClientUrl%>">
								                  <embed width="100%" height="100%"></embed>
								                </OBJECT>
									          </td>
									  		</tr>
									        <tr>
										        <td bgcolor=menu height='20' colspan="2">
													<div id=StatusBar>状态栏</div>
										        </td>
									        </tr>
								        </table>
									  </td>
									</tr>
								 </table>
							  </form>
						    </div>
						</div>
					</div>
				</section>
		    </div>
		</div>
	</body>
	<script type="text/javascript">
        var ctxPath = "${pageContext.request.contextPath}";
    </script>
    <script src="${pageContext.request.contextPath}/scripts/lib/jquery/jquery.min.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/lib/jquery-ui/jquery-ui-1.10.3.custom.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/lib/bootstrap/bootstrap.min.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/dialog.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/util.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/progress-bar.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/global.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/shade-layer.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/loading.js" ></script>

	<script src="${pageContext.request.contextPath}/scripts/lib/slimScroll/jquery.slimscroll.min.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/adminLTE.min.js" ></script>

	<!-- 表单验证脚本开始-->
	<script src="${pageContext.request.contextPath}/scripts/lib/jquery-form/jquery.form.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/lib/jquery-validation/jquery.validate.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/lib/jquery-validation/extendValidate.js" ></script>
	<script src="${pageContext.request.contextPath}/scripts/form.js" ></script>
	<!-- 表单验证脚本结束-->

</html>